package com.lute.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.ide.eclipse.quickfix.jdt.processors.RequestMappingDialog.Method;
import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.json.UserNameJSON;
import com.lute.model.Client;
import com.lute.model.ClientAdminProfile;
import com.lute.model.User;
import com.lute.utils.ServerErrorResponse;

@Controller
public class GetClientsServlet {
	
	@RequestMapping(value="/getClients", method= RequestMethod.GET)
	public @ResponseBody String getRegisteredUsers(HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonRes = new JSONObject();
		
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
//		if(!(empty.equals(""))) {
//			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
//			result = jsonRes.toString();
//			return result;
//		}

		int sessionUserId = (Integer)session.getAttribute("sessionUserId");
		String role = (String)session.getAttribute("role");
		//String role ="admin";
		/* check the authorization */
		if(!((role.equals("clientAdmin")) || (role.equals("admin")))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		List<JSONObject> basicClientData = new ArrayList<JSONObject>();
		
		try {

			List<Client> clients = new ArrayList<Client>();
			if(role.equals("clientAdmin")) {
				ClientAdminProfile clientAdminProfile = ClientAdminProfile.
						getClientAdminProfileByClientAdminIdWithClientsFromDB(sessionUserId);
				clients = clientAdminProfile.getClients();
				
			} else {
				clients = Client.getClientsFromDB();
			}
			
			for(Client client: clients) {
				JSONObject obj = new JSONObject();
				obj.put("clientName", client.getName());
				obj.put("clientId", client.getId_client());
				basicClientData.add(obj);
			}
		}catch(NullPointerException npe) {
			jsonRes.put("result", ServerErrorResponse.INTERNAL_ERROR.toString());
			result = jsonRes.toString();
			return result;
		}
			
		jsonRes.put("clients", basicClientData);
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		System.out.println(result);
		return result;
	}
}
